import java.util.ArrayList;
import java.util.List;

public class leetcode6_N字形变换 {
    public static void main(String[] args) {
        Solution19 solution19 = new Solution19();
        System.out.println(solution19.convert("PAYPALISHIRING", 4));
    }
}

class Solution19 {
    public String convert(String s, int numRows) {
        if (numRows <= 1) {
            return s;
        }
        List<List<String>> list = new ArrayList<>();
        for (int i = 0; i < numRows; i++) {
            list.add(new ArrayList<>());
        }
        int flag = 1;
        int listIdx = 0;
        for (int i = 0; i < s.length(); i++) {
            list.get(listIdx).add(String.valueOf(s.charAt(i)));
            listIdx += flag;
            if (listIdx == 0 || listIdx == numRows - 1) flag = -flag;
        }
        String ans = "";
        for (List<String> stringList : list) {
            ans += String.join("", stringList);
        }
        return ans;
    }
}